package com.kis.bot.utils.api

import com.alibaba.fastjson.JSONObject
import com.kis.bot.enums.StringEnums
import org.apache.http.NameValuePair
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClients
import org.apache.http.message.BasicNameValuePair

/**
 * Created by wongloong on 16-9-2.
 */
object Http {
    fun post(url: String, param: Map<String, String>): CloseableHttpResponse {
        System.setProperty("https.protocols", "TLSv1");
        System.setProperty("jsse.enableSNIExtension", "false")
        val client = HttpClients.createSystem()
        val post = HttpPost(url)
        post.setHeader("User-agent", StringEnums.USERAGENT.value)
        val params = arrayListOf<NameValuePair>()
        for ((k, v) in param) {
            params.add(BasicNameValuePair(k, v))
        }
        post.entity = UrlEncodedFormEntity(params)
        val response = client.execute(post)
        return response
    }

    fun post(url: String, json: JSONObject): CloseableHttpResponse {
        System.setProperty("https.protocols", "TLSv1");
        System.setProperty("jsse.enableSNIExtension", "false")
        val client = HttpClients.createSystem()
        val post = HttpPost(url)
        post.setHeader("User-agent", StringEnums.USERAGENT.value)
        post.entity = StringEntity(json.toString(), "UTF8")
        val response = client.execute(post)
        return response
    }

    fun get(url: String): CloseableHttpResponse {
        System.setProperty("https.protocols", "TLSv1");
        System.setProperty("jsse.enableSNIExtension", "false")
        val client = HttpClients.createSystem()
        val get = HttpGet(url)
        get.setHeader("User-agent", StringEnums.USERAGENT.value)
        val response = client.execute(get)
        return response
    }
}